package com.spring.sunflower.base;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.spring.sunflower.util.LogUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.text.ParseException;

import io.reactivex.rxjava3.observers.DisposableObserver;
import retrofit2.HttpException;

public abstract class BaseObserver<T> extends DisposableObserver<T> {
    protected BaseView view;
    /**
     * 网络连接失败  无网
     */
    public static final int NETWORK_ERROR = 100000;
    /**
     * 解析数据失败
     */
    public static final int PARSE_ERROR = 1008;
    /**
     * 网络问题
     */
    public static final int BAD_NETWORK = 1007;
    /**
     * 连接错误
     */
    public static final int CONNECT_ERROR = 1006;
    /**
     * 连接超时
     */
    public static final int CONNECT_TIMEOUT = 1005;
    /**
     * 其他所有情况
     */
    public static final int NOT_TRUE_OVER = 1004;

    public BaseObserver(BaseView view) {
        this.view = view;
    }

    public BaseObserver() {
    }

    @Override
    protected void onStart() {
        if (view != null) {
            view.showLoading();
        }
    }

    @Override
    public void onNext(T o) {
//
//        if (view != null) {
//            view.hideLoading();
//        }
        String json = new Gson().toJson(o);
        LogUtils.INSTANCE.logW("BasePresenter", "json=" + json);
        try {
            JSONObject j = new JSONObject(json);
            if (j.has("status")) {
                String status1 = j.getString("status");
                if (status1.equals("1")) {
                    onSuccess(o);
                } else if (status1.equals("0")) {
                    if (j.has("message")) {
                        String message = j.getString("message");
                        onError(0, message);
                        showErrorInfo(0, message);

                    } else {
                        onError(0, "请求失败");
                        showErrorInfo(0, "请求失败");
                    }
                } else if (status1.equals("-1")) {
                    if (view != null) {
                        view.showTokenExpired();
                    }
                } else if (status1.equals("-2")) {
                    onError(-2, "余额不足");
                    showErrorInfo(-2, "余额不足");
                    if (view != null) {
                        view.showNeedCharge();
                    }
                } else {
                    if (j.has("message")) {
                        String message = j.getString("message");
                        onError(Integer.valueOf(status1), message);
                        showErrorInfo(Integer.valueOf(status1), message);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private void showErrorInfo(int status, String msg) {
        if (view != null) {
            view.showError(status, msg);
        }
    }

    @Override
    public void onError(Throwable e) {
        if (view != null) {
            view.hideLoading();
        }
        if (e instanceof HttpException) {
            //   HTTP错误
            onException(BAD_NETWORK, e.getMessage());
        } else if (e instanceof ConnectException
                || e instanceof UnknownHostException) {
            //   连接错误
            onException(CONNECT_ERROR, e.getMessage());
        } else if (e instanceof InterruptedIOException) {
            //  连接超时
            onException(CONNECT_TIMEOUT, e.getMessage());
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            //  解析错误
            onException(PARSE_ERROR, e.getMessage());
            e.printStackTrace();
        } else {
            if (e != null) {
                onError(-100, e.toString());
            } else {
                onError(-100, "未知错误");
            }
        }
    }

    private void onException(int unknownError, String message) {
        switch (unknownError) {
            case CONNECT_ERROR:
                onError(CONNECT_ERROR, "连接错误");
                break;
            case CONNECT_TIMEOUT:
                onError(CONNECT_TIMEOUT, "连接超时");
                break;
            case BAD_NETWORK:
                onError(BAD_NETWORK, "网络超时");
                break;
            case PARSE_ERROR:
                onError(PARSE_ERROR, "数据解析失败");
                break;
            //非true的所有情况
            case NOT_TRUE_OVER:
                onError(NOT_TRUE_OVER, message);
                break;
            default:
                break;
        }
        showErrorInfo(unknownError, message);
    }

    //消失写到这 有一定的延迟  对dialog显示有影响
    @Override
    public void onComplete() {
        if (view != null) {
            view.hideLoading();
        }
    }

    public abstract void onSuccess(T bean);

    public abstract void onError(int status, String msg);
}